﻿using BCCServer.Common.Common;
using BCCServer.DAO.chat;
using BCCServer.DAO.commodity;
using BCCServer.DAO.Interfaces;
using BCCServer.IService.IChatService;
using BCCServer.Model.Enums;
using BCCServer.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCCServer.Service.CommodityService
{
    public class Commodity : ICommodity
    {
        private readonly CommodityDB commodityDB;

        public Commodity(CommodityDB commodity)
        {
            this.commodityDB = commodity;
        }

        /// <summary>
        /// 查询商品信息
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<SCommodity>> selCommodityAsync(int pageNumber = 1, int pageSize = 20)
        {
            return await commodityDB.selCommodityAsync(pageNumber, pageSize);
        }


        /// <summary>
        /// 库存减一
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool reduceCommodity(int Id)
        {
            string luaScript = @"
    local stock = redis.call('GET', KEYS[1])
    if tonumber(stock) > 0 then
        redis.call('SET', KEYS[1], stock - 1)
        return 1
    else
        return 0
    end";
            string productKey = $"product:{Id}";
            var rs = RedisHelper.ScriptEvaluateLua(luaScript, productKey);
            if (rs)
            {
                #region 弃用方案一
                //var rs2 = commodityDB.reduceCommodity(Id);
                //if (rs2 == (int)ResEnums.成功)
                //{
                //    return true;
                //}
                //else if (rs2 == (int)ResEnums.失败)
                //{
                //    for (int i = 0; i < 3; i++)//重试3次
                //    {
                //        rs2 = commodityDB.reduceCommodity(Id);
                //        if (rs2 == (int)ResEnums.成功)
                //        {
                //            return true;
                //        }
                //    }

                //    //加入消息队列 稍后再扣除mysql库存
                //}
                //else //if (rs2 == (int)ResEnums.异常)
                //{
                //    //加入消息队列 稍后再扣除mysql库存
                //}
                #endregion

                #region 方案二 扣减库存后mq写入消息

                #endregion
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="sShoppingcart"></param>
        /// <returns></returns>
        public int addShoppingcart(SShoppingcartInput shoppingcart)
        {
            SShoppingcart sShoppingcart = new SShoppingcart();
            sShoppingcart.CommodityId = shoppingcart.CommodityId;
            sShoppingcart.CommodityNum = shoppingcart.CommodityNum;
            sShoppingcart.SuserId = shoppingcart.SuserId;
            sShoppingcart.IsDelete = false;
            sShoppingcart.CreateTime = sShoppingcart.UpdateTime = DateTime.Now;
            return commodityDB.addShoppingcart(sShoppingcart);
        }
    }
}
